package com.euond.accv.gateway.web;

import com.euond.accv.gateway.domain.CustomerDevManager;
import com.euond.accv.gateway.service.CustomerDevManagerService;
import com.euond.accv.gateway.service.TokenService;
import com.euond.accv.gateway.utils.TokenUtils;
import com.euond.common.domain.search.Searchable;
import com.euond.common.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cgi-bin")
public class CustomerDevManagerController {

	@Autowired
	private CustomerDevManagerService service;

	@Autowired
	private TokenService tokenService;

	@RequestMapping("/create/token.json")
	@ResponseBody
	public Map<String, Object> createToken(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String appId = request.getParameter("appId");
		String appSecret = request.getParameter("appSecret");
		String deviceNo = request.getParameter("deviceNo");

		Map<String, Object> resultMap = new HashMap<>();

		if(StringUtils.isEmpty(appId) || StringUtils.isEmpty(appSecret)){
			ResultVO flag = new ResultVO();
			flag.setRetMsg("【appId】【appSecret】不允许为空!");
			flag.setRetCode("1001");
			resultMap.put("flag", flag);
			return resultMap;
		}
		String accessToken = createAccessToken(appId,appSecret,deviceNo);

		if(StringUtils.isEmpty(accessToken)){
			ResultVO flag = new ResultVO();
			flag.setRetMsg("[appId:"+appId+"],[appSecret:"+appSecret+"]不存在!");
			flag.setRetCode("1002");
			resultMap.put("flag", flag);
			return resultMap;
		}

		ResultVO flag = new ResultVO();
		Map<String,String> tokenInfo = new HashMap<>();
		tokenInfo.put("accessToken",accessToken);
		tokenInfo.put("expire","7200");

		flag.setRetDetail(tokenInfo);
		flag.setRetCode("0");
		resultMap.put("flag", flag);
		return resultMap;
	}

//	@RequestMapping("/findAll.json")
//	@ResponseBody
//	public Map<String, Object> audit(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		Map<String, Object> resultMap = new HashMap<>();
//		ResultVO flag = new ResultVO();
//
//		List<CustomerDevManager> list = tokenService.findAll();
//		flag.setRetDetail(list);
//		flag.setRetCode("0");
//		resultMap.put("flag", flag);
//		return resultMap;
//	}


	/**
	 * 验证客户端身份，生成令牌
	 * @param appId 开发者ID
	 * @param appSecret 开发者密码
	 * @param deviceNo 设备编号
	 * @return
	 */
	public String createAccessToken(String appId,String appSecret,String deviceNo){
		Searchable searchable = Searchable.newSearchable();
		searchable.addSearchParam("devId_eq",appId);
		searchable.addSearchParam("devPassword_eq",appSecret);
		searchable.addSearchParam("status_eq","1");

		List<CustomerDevManager> customerDevAuthorizations = service.findAll(searchable).getContent();
		if(customerDevAuthorizations.size() == 0){
			return null;
		}

		CustomerDevManager customerDevManager = customerDevAuthorizations.get(0);
		String accessToken = TokenUtils.createToken(appId,appSecret,customerDevManager.getCreateTime(),deviceNo);

		//AccessToken
		tokenService.push(accessToken,customerDevManager,deviceNo);

		return accessToken;
	}
}